<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" v-if="checkPermission(['goodsExchange:add'])" type="primary"
            @click="handleExportGoodsExchange">导出</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" @advancedSearch="advancedSearchEvent" placeholder="请输入换货编号" advanced
            advancedWidth="465">
            <template slot="advanced-content">
              <el-form ref="advancedForm" :model="advancedForm" label-width="90px">
                <el-form-item label="所属部门">
                  <el-cascader v-model="advancedForm.dept_id" :options="deptList" :props="{
                    label: 'name',
                    value: 'id',
                    emitPath: false,
                    checkStrictly: true
                  }" clearable></el-cascader>
                </el-form-item>
                <el-form-item label="仓库">
                  <el-select v-model="advancedForm.warehouse_id">
                    <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
                <el-form-item label="商品名称" prop="product_name">
                  <el-input v-model="advancedForm.product_name" :minlength="2" :maxlength="30" clearable
                    placeholder="请输入商品名称"></el-input>
                </el-form-item>
                <el-form-item label="商品类别" prop="category_id">
                  <el-cascader v-model="advancedForm.category_id" :options="categorys" :props="{
                    label: 'name',
                    value: 'category_id',
                    emitPath: false,
                    checkStrictly: true
                  }" clearable></el-cascader>
                </el-form-item>
                <el-form-item label="调换类型">
                  <el-select v-model="advancedForm.type">
                    <el-option key="0" label="退货" value="0" />
                    <el-option key="1" label="换货" value="1" />
                  </el-select>
                </el-form-item>
                <el-form-item label="时间">
                  <el-date-picker v-model="advancedForm.goodsExchange_time_range" type="daterange" align="center"
                    :editable="false" unlink-panels :clearable="false" :default-time="['00:00:00', '23:59:59']"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="timestamp"
                    :picker-options="{ disabledDate(time) { return time.getTime() - 1 >= new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()).getTime() + 86400000 - 1 }, shortcuts: MixinPickerShortcuts }">
                  </el-date-picker>
                </el-form-item>
              </el-form>
            </template>
          </en-table-search>
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column prop="sn" label="换货单编号">
          <template slot-scope="scope">
            <router-link
              v-if="checkPermission(['goodsExchange'])"
              style="color: #409eff;"
              :to="{ name: 'goodsExchange', params: { id: scope.row.change_id } }">
              {{ scope.row.sn }}
            </router-link>
            <span v-else>{{scope.row.sn}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="stock_sn" label="入库单编号" />
        <el-table-column prop="change_time" label="换货日期">
          <template slot-scope="scope">{{ scope.row.change_time | unixToDate }}</template>
        </el-table-column>
        <el-table-column prop="product_sn" label="商品编号" />
        <el-table-column prop="product_name" label="商品名称" />
        <el-table-column prop="category_name" label="商品类别" />
        <el-table-column prop="specification" label="规格型号" />
        <el-table-column prop="unit" label="单位" />
        <el-table-column label="调换类型">
          <template slot-scope="{ row }">{{ row.type === 0 ? '退货' : '换货' }}</template>
        </el-table-column>
        <el-table-column prop="num" label="调换数量" />
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
  </div>
</template>

<script>
import * as API_Statistics from '@/api/statistics'
import * as API_Setting from '@/api/setting'
import { Foundation } from '~/ui-utils'
import * as API_BasicSetting from '@/api/basicSetting'
import * as API_Category from '@/api/category'
const FileDownload = require('js-file-download')
export default {
  name: 'goodsExchangeStatistics',
  data() {
    return {
      categorys: [],
      deptList: [],
      warehouseList: [],
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 高级搜索数据
      advancedForm: {},
      // 列表数据
      tableData: '',
      dialogVisible: false
    }
  },
  mounted() {
    this.GET_DeptList()
    this.GET_WarehouseList()
    this.GET_Category()
  },
  watch: {
    $route: {
      immediate: true,
      handler(newVal) {
        this.params.page_no = 1
        if (newVal.params.product_name) {
          this.params.product_name = newVal.params.product_name
          this.advancedForm.product_name = newVal.params.product_name
        }
        this.GET_GoodsExchangeList()
      }
    }
  },
  methods: {
    /** 获取类别 */
    GET_Category() {
      API_Category.getCategoryAllChildren(0).then(response => {
        this.categorys = response
      })
    },
    /** 获取仓库 */
    GET_WarehouseList() {
      API_BasicSetting.getWarehouseListAll().then(response => {
        this.warehouseList = response
      })
    },
    /** 获取部门 */
    GET_DeptList() {
      API_Setting.getDeptList().then(response => {
        this.deptList = Foundation.buildTree(response, '0')
      })
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_GoodsExchangeList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_GoodsExchangeList()
    },

    /** 导出 */
    handleExportGoodsExchange() {
      API_Statistics.getChangeFormExport(this.params).then(response => {
        FileDownload(response, '商品换货统计.xlsx')
      })
    },
    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        sn: data
      }
      Object.keys(this.advancedForm).forEach((key) => delete this.params[key])
      this.params.page_no = 1
      this.GET_GoodsExchangeList()
    },
    /** 高级搜索事件触发 */
    advancedSearchEvent() {
      this.params = {
        ...this.params,
        ...this.advancedForm
      }
      delete this.params.start_time
      delete this.params.end_time
      if (this.advancedForm.goodsExchange_time_range) {
        this.params.start_time = parseInt(Number(this.advancedForm.goodsExchange_time_range[0]) / 1000)
        this.params.end_time = parseInt(Number(this.advancedForm.goodsExchange_time_range[1]) / 1000)
      }
      delete this.params.goodsExchange_time_range
      this.params.page_no = 1
      this.GET_GoodsExchangeList()
    },
    /** 获取列表 */
    GET_GoodsExchangeList() {
      this.loading = true
      API_Statistics.getChangeFormStatistics(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.print {
  margin-left: 790px;
}

.tips-t {
  display: flex;
  padding: 10px 0;

  div {
    flex: 1;
    text-align: left;
    padding-left: 70px;
  }
}

.tips-f {
  padding: 20px 0;
}

.face-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
